package com.person.jc.tag;

import java.math.BigDecimal;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.person.jc.utils.NumHelper;
import com.person.jc.utils.Utils;

/*******************************************************************************
 * @程序名: SearchInput.java
 * @日期 : 2014-5-9
 * @作者 : 武智雄 (wuzhixiong@vvise.com)
 * @模块 : [项目]-[一级菜单]-[二级菜单]-[查询条件中input]
 * @描述 : [功能简介] ------------------------------------------------------------
 *     修改历史 序号 日期 修改人 修改原因 1 2
 ******************************************************************************/
public class SearchInput extends BodyTagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String id;
	private String name;
	private String endId;
	private String endName;
	private String displayText;
	private String defVal;
	private String endDefVal;
	private String type;// 1 text（默认）；2单个时间选择 time；3时间段
	// time2（传值时可使用1、2、3也可使用text、time、time2）
	private String style;
	private String dateFormat;// 时间格式化
	private String clazz; //文本框样式
	private String colspan;//合并行数
	private String groupFlag;//组合标识
	
	private String dateLimit;//时间限制类型 y M d
	private String limitNum;//限制数字
	private String isShowClear;//是否显示清空按钮
	
	private String isShowToday;//是否显示今天

	/**
	 * 
	 * @Description: [拼接HTML]
	 * @Author: 武智雄 (wuzhixiong@vvise.com)
	 * @Date: 2014-5-9
	 * @@return
	 */
	private String splitHtml() {
		StringBuffer html = new StringBuffer();
		
		if (Utils.isEmpty(type) || "1".equals(type)
				|| "text".equals(type.toLowerCase()))
			type = "1";
		else if ("2".equals(type) || "time".equals(type.toLowerCase()))
			type = "2";
		else if ("3".equals(type) || "time2".equals(type.toLowerCase()))
			type = "3";

		String defId = "beginDate";
		String defId2 = "endDate";
		if (Utils.isEmpty(id))
			id = defId;
		if (Utils.isEmpty(name))
			name = defId;
		if (Utils.isEmpty(endId))
			endId = defId2;
		if (Utils.isEmpty(endName))
			endName = defId2;
		if(Utils.isEmpty(dateFormat))
			dateFormat = "yyyy-MM-dd";
		
		if(Utils.isNotEmpty(displayText))
		    displayText += "：";
		else
		    displayText = "";
		
		int colW = 210;
		int leftW = displayText.length() * 14;
//		int mLeft = 210 - displayText.length() * 14;//一个文字14px 总共210px
		
		boolean ifGroup = false;
		if(Utils.isNotEmpty(groupFlag) && "true".equals(groupFlag.toLowerCase())){
		    ifGroup = true;
		}
		
		if(ifGroup){
		    leftW += 20;
        }
		
		//是否显示清空按钮
		boolean ifShowClear = true;
		if(Utils.isNotEmpty(isShowClear) && "false".equals(isShowClear.toLowerCase())){
		    ifShowClear = false;
		}
		
		if("1".equals(type)){
		    if(!ifGroup){
    			html.append("<div class=\"input_insert f_l border_1\"");
    			if(Utils.isNotEmpty(colspan)){
    			    colW = new BigDecimal(colW).multiply(NumHelper.getBigDecimalByStr(colspan)).intValue();
    			    colW += 18 * (NumHelper.getBigDecimalByStr(colspan).intValue() - 1);//边距
    			    colW += 10;//内边距
    			    html.append(" style=\"width:"+ colW +"px;\"");
    			}
    			html.append(">");
    			html.append("<label class=\"left_desc\" style=\"width:"+leftW+"px;\">" + displayText + "</label>");
		    }
		}else if("2".equals(type)){
			html.append(" <div class=\"time_select_x f_l border_1\">");
			html.append("<label class=\"time_x_first_text\" style=\"width:"+leftW+"px;\">" + displayText+ "</label>");
		}else if("3".equals(type)){
		    if(!ifGroup){
    			html.append(" <div class=\"time_select_x f_l border_1\">");
    			html.append("<label class=\"time_x_first_text\" style=\"width:"+leftW+"px;\">" + displayText+ "</label>");
		    }
		}
		
		//---使用div方式
		html.append("<div style=\"width:100%;float:right;margin-left:-"+ leftW +"px;\">");
		html.append("<div style=\"margin-left:"+ leftW +"px;\">");
		
		html.append("<input type=\"text\" id=\"" + id + "\" name=\"" + name+ "\"");
		String endHtml = "";
		if (Utils.isNotEmpty(defVal))
			html.append(" value=\"" + defVal + "\"");
		
		if ("1".equals(type)) {
			html.append(" class=\"inputs_func ");
			if(Utils.isNotEmpty(clazz)){
				html.append(" "+ clazz + " ");
			}
			html.append("\"");
		} else if ("2".equals(type)) {
			html.append(" class=\"Wdate\" onFocus=\"WdatePicker({dateFmt:'"+ dateFormat + "\'");
			if(!ifShowClear){
			    html.append(",isShowClear:false,readOnly:true");
			}
			
			if(Utils.isNotEmpty(isShowToday) && "false".equals(isShowToday.toLowerCase())){
			    html.append(",isShowToday:false");
			}
		} else if ("3".equals(type)) {
			html.append(" class=\"Wdate\"");
			html.append(" onFocus=\"WdatePicker({maxDate:'#F{$dp.$D(\\'" + endId + "\\')}'");
//			if(!"yyyy-MM-dd".equals(dateFormat)){
//			    html.append(",startDate:'%y-%M-01 00:00:00',alwaysUseStartDate:true");
//			}
			if(Utils.isNotEmpty(dateLimit)){
			    html.append(",minDate:'#F{$dp.$D(\\'" + endId+ "\\',{"+ dateLimit +":-"+ limitNum +"})}'");
			}
			if(!ifShowClear){
                html.append(",isShowClear:false,readOnly:true");
            }
			
			if(Utils.isNotEmpty(isShowToday) && "false".equals(isShowToday.toLowerCase())){
                html.append(",isShowToday:false");
            }
			
			html.append(",dateFmt:'"+ dateFormat + "\'})\"");
			
			endHtml += "</div>";
			
			endHtml += "<label class=\"time_x_sec_text\">-</label>";
			endHtml += "<div class=\"time_select_x f_l border_1\">";
			endHtml += "<input type=\"text\" id=\"" + endId + "\" name=\""+ endName + "\"" +
			        " class=\"Wdate time_x_border_r\"" +
					" onFocus=\"WdatePicker({minDate:'#F{$dp.$D(\\'" + id + "\\')}' ";
			
			if(Utils.isNotEmpty(dateLimit)){
			    endHtml += ",maxDate:'#F{$dp.$D(\\'" + id+ "\\',{"+ dateLimit +":"+ limitNum +"})}'";
            }
			if(!ifShowClear){
			    endHtml += ",isShowClear:false,readOnly:true";
            }
			if(Utils.isNotEmpty(isShowToday) && "false".equals(isShowToday.toLowerCase())){
			    endHtml += ",isShowToday:false";
            }
			
			endHtml += ",dateFmt:'"+ dateFormat + "\'})\"";
			
			if (Utils.isNotEmpty(endDefVal))
				endHtml += " value=\"" + endDefVal + "\"";
			if (Utils.isNotEmpty(style))
				endHtml += " style=\"width: 100%;" + style + "\"";
			else
			    endHtml += " style=\"width: 100%;\"";
			
			endHtml += " />";
		}
		
        if (Utils.isNotEmpty(style))
            html.append(" style=\"width: 100%;" + style + "\"");
        else
            html.append(" style=\"width: 100%;\"");
        
		html.append(" />");
		//---使用div方式
		html.append("</div>");
		html.append("</div>");
		html.append(endHtml);
		if(!ifGroup){
		    html.append("</div>");
		}
		return html.toString();
	}

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.write(splitHtml());
		} catch (Exception e) {
			throw new JspException("Error:" + e.getMessage());
		}
		return EVAL_BODY_INCLUDE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDisplayText() {
		return displayText;
	}

	public void setDisplayText(String displayText) {
		this.displayText = displayText;
	}

	public String getDefVal() {
		return defVal;
	}

	public void setDefVal(String defVal) {
		this.defVal = defVal;
	}

	public String getEndDefVal() {
		return endDefVal;
	}

	public void setEndDefVal(String endDefVal) {
		this.endDefVal = endDefVal;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getEndId() {
		return endId;
	}

	public void setEndId(String endId) {
		this.endId = endId;
	}

	public String getEndName() {
		return endName;
	}

	public void setEndName(String endName) {
		this.endName = endName;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

    public String getColspan() {
        return colspan;
    }

    public void setColspan(String colspan) {
        this.colspan = colspan;
    }

    public String getGroupFlag() {
        return groupFlag;
    }

    public void setGroupFlag(String groupFlag) {
        this.groupFlag = groupFlag;
    }

    public String getDateLimit() {
        return dateLimit;
    }

    public void setDateLimit(String dateLimit) {
        this.dateLimit = dateLimit;
    }

    public String getLimitNum() {
        return limitNum;
    }

    public void setLimitNum(String limitNum) {
        this.limitNum = limitNum;
    }

    public String getIsShowClear() {
        return isShowClear;
    }

    public void setIsShowClear(String isShowClear) {
        this.isShowClear = isShowClear;
    }

    public String getIsShowToday() {
        return isShowToday;
    }

    public void setIsShowToday(String isShowToday) {
        this.isShowToday = isShowToday;
    }

}
